package com.aaa.util;

import com.aaa.entity.Orgnization;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：Teacher陈
 * @date ：Created in 2020/7/26 11:03
 * @description：字符串转json
 * @modified By：
 * @version: 1.0
 */
public class JsonUtil {

    /**
     * @create by: Teacher陈
     * @description: json字符串转java对象
     * @create time: 2020/7/26 11:09
     * @param fileName
     * @return Object
     */
    public static Object fromJsonToObject(String fileName) throws Exception {
/*        File file = new File(fileName);
        FileInputStream fileInputStream = new FileInputStream(file);
        BufferedReader bf = new BufferedReader(new InputStreamReader(fileInputStream));
        String line;
        StringBuilder stringBuilder = new StringBuilder();
        while ((line = bf.readLine()) != null) {
            stringBuilder.append(line);
        }*/
        ObjectMapper objectMapper = new ObjectMapper();
        //String content, Class<T> valueType
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, Orgnization.class);
        //List<Orgnization> orgnizationList = objectMapper.readValue(stringBuilder.toString(), javaType);
        List<Orgnization> orgnizationList = objectMapper.readValue(new File(fileName), javaType);
        System.out.println(orgnizationList.size());
        return orgnizationList;

    }

    public static void main(String[] args) throws Exception {
        fromJsonToObject("D:\\org.json");
    }
}
